package com.example.multilevelapprovalback.controller;
import com.example.multilevelapprovalback.pojo.Result;
import com.example.multilevelapprovalback.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.time.Year;
import java.util.List;
@Slf4j
@RestController
public class FileController {
    @Autowired
    private FileService fileService;
    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping ("/upload")
    public Result upload(MultipartFile[] file) {
        log.info("文件：{}",file.length);
        if(file != null && file.length != 0) {
            List<String> path = fileService.upload(file);
            return Result.success(path);
        }
        return Result.error("上传文件为空");
    }
    /**
     * 文件下载
     * @param path
     * @param response
     */
    @GetMapping("/download")
    public void download(String path,HttpServletResponse response) {
        fileService.download(path,response);
    }
    /**
     * 删除文件
     * @param path
     * @return
     */
    @GetMapping("/delete")
    public Result delete(String path) {
        boolean result = fileService.delete(path);
        if(result) return Result.success();
        else return Result.error("删除文件失败");
    }
}
